<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

/**
 * 组件
 *
 * @Desc:验证请求类
 *
 * @author: zhx
 *
 * @Time: 2023/8/10 10:37
 */
class SyscomRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 获取应用于请求的验证规则。
     *
     * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
     */
    public function rules()
    {
        return [
            'code' => 'required|string|max:50',
            'parent_code' => 'required|int',
            'title' => 'required|string|max:50',
            'type' => 'required|string|max:50',
            'body' => 'required|string',
            'desc' => 'required|string|max:500',
        ];
    }

    public function messages(): array
    {
        return [
            'code.required' => '组件编码是必填字段。',
            'code.max' => '组件编码不能超过 50 个字符。',
            'title.max' => '组件标题不能超过 50 个字符。',
            'type.required' => '组件类型是必填字段。',
            'type.max' => '组件类型不能超过 50 个字符。',
            'tables.required' => '组件操作表是必填字段。',
            'tables.max' => '组件操作表不能超过 200 个字符。',
            'pri_field.max' => '操作字段不能超过 64 个字符。',
            'body.required' => '组件Body体是必填字段。',
            'desc.max' => '组件描述不能超过 500 个字符。',
        ];
    }

    public function all($keys = null)
    {
        $data = parent::all($keys);

        return $data;
    }

    protected function failedValidation(Validator $validator)
    {
        $error = $validator->errors()->all();
        throw new HttpResponseException(response()->json(['msg' => $error[0], 'code' => '500', 'data' => 'error'], 500));
    }
}
